<template>
<view>
	<image class="banner" :src="imgUrl+info.img"></image>
	<view class="tclass" v-if="info.id == 24">
		<view class="title">{{info.name}}</view>
	</view>
	<view class="tclass" v-else>
		<view class="title">{{info.name}}</view>
		<view class="remark">1积分 = 1元人民币</view>
	</view>
	
	<view class="pclass" v-if="info.id == 24">
		<view>积分:¥:{{info.price}}</view>
		<view class="remark">积分兑换红包</view>
		<button open-type="share" class="myshare">分享</button>
	</view>
	<view class="pclass" v-else>
		<view>价格:¥:{{info.price}}</view>
		<view class="remark">消费满500免邮</view>
		<button @click="share" class="myshare">分享</button>
	</view>
	
	<view class="content">
		<view class="ctitle">商品简介</view>
		<rich-text v-if="info.content" :nodes="info.content"></rich-text>
	</view>
	<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup"  
	@click="goCart" @buttonClick="buttonClick" />
	
	<!-- <uni-popup ref="popup" type="bottom">
		<view>购物车</view>
	</uni-popup> -->
</view>
</template>
<script>
import request from '@/util/request.js'
import uniGoodsNav from '@/modules/uni-goods-nav/uni-goods-nav.vue'
//import uniPopup from '@/modules/uni-popup/uni-popup.vue'
export default {
	data() {
		return {
			imgUrl:this.url,
			info:{},
			visible: '',
			options: [
				{
			        icon: '/static/icon/score.png',
			        text: '购物车',
			        info: 0
			    },
			],
			buttonGroup: [
				{
			        text: '加入购物车',
			        backgroundColor: '#ff0000',
			        color: '#fff'
			    },{
					text: '立即购买',
					backgroundColor: '#ffa200',
					color: '#fff'
				}
			]
		}
	},
	components: {uniGoodsNav},
	onLoad:function(e){
		uni.showLoading({  
		    title: '加载中'  
		});
		this.getInfo(e.id)
		
		if(e.id == 24){
			this.buttonGroup = [
				{
					text: '立即购买',
					backgroundColor: '#ffa200',
					color: '#fff'
				}
			]
		}
		this.getCartNum()
	},
	methods: {
		getCartNum:function(){
			var that = this;
			var data = {uid:uni.getStorageSync('uid')}
			request({
				url: 'cart/count',
				method: 'get',
				data:data,
				success: (res) => {
					that.options[0].info = res.data.data
				}
			})
		},
		getInfo:function(id){
			var that = this;
			var data = {id:id}
			request({
				url: 'score/info',
				method: 'get',
				data:data,
				success: (res) => {
					uni.hideLoading();
					that.info = res.data.data
					that.info.content = res.data.data.content
						.replace(/\<img src="/gi, '<img src="'+this.imgUrl);
					that.info.content = res.data.data.content
						.replace(/<img([\s\w"-=\/\.:;]+)/ig, '<img style="width: 100%;" $1')
				}
			})
		},
		onReachBottom: function(){
			if(this.pageShow){
				 //当前不是最后一页可加载
				 this.page = this.page + 1
				 uni.showLoading()
				 this.getList();
			}
		},
		getList:function(){
			var that = this;
			var data = {page:this.page}
			request({
				url: 'factory/list',
				method: 'get',
				data:data,
				success: (res) => {
					if(res.data.code == 1){
						if(res.data.data.last_page <= that.page){
							that.pageShow = false
						}
						if(that.page == 1){
							that.list = res.data.data.data
						}else{
							var list = that.list
							that.list = list.concat(res.data.data.data)
						}
					}
				}
			})
		},
		share:function(){
			var img = this.imgUrl + this.info.img
			uni.share({
			    provider: "weixin",
			    scene: "WXSceneSession",
			    type: 5,
			    imageUrl:img,
			    title:this.info.name,
				miniProgram: {
					id: 'gh_42fb5eed1670',
					path: 'pages/index/index',
					type: 0,
					webUrl: 'https://www.tymyztc.com'
				},
				success: ret => {
					console.log(JSON.stringify(ret));
				}
			});
		},
		goCart:function(e) {
			uni.navigateTo({
			  url:'./cart'
			})
		 },
		buttonClick:function (e){
			var index = e.index
			if(index == 0){
				this.options[0].info++
				//加入购物车
				var that = this;
				var data = {
					uid:uni.getStorageSync('uid'),
					goods_id:this.info.id
				}
				request({
					url: 'cart/add',
					method: 'post',
					data:data,
					success: (res) => {
						uni.showToast({
							title: res.data.msg,
							icon: "success", 
							position:'bottom',
						});
						return;
					}
				})
			}else{
				//下单购买
				//this.$refs.popup.open('center')
				uni.navigateTo({
					url:'./order?str='+this.info.id+'&num=1'
				})
			}
		}
	}
}
</script>
<style>
.banner {
	width: 100%;
	height:500rpx;
	border-bottom: 1rpx solid #000000;
}
.tclass{
	display:flex;
	justify-content: row;
	width:100%;height:30px;
	line-height: 30px;
}
.title{
	padding-left:20rpx;
	font-weight:bold;
}
.remark{
	font-size:12px;
	margin-left:50rpx;
	color:#b3b3b3
}
.pclass{
	display: flex;
	justify-content: space-between;
	height:30px;
	line-height: 30px;
	font-size:13px;
	padding:10rpx 0  10rpx 20rpx;
	border-bottom: 10rpx solid #e6e6e6;
}
.myshare{
	background-color: #cc0000;
	font-size:10px;
	color:#fff;
	height:50rpx;
	line-height: 50rpx;
}
.content{
	width:100%;
	padding:10rpx 10rpx 10rpx 10rpx;
}
.ctitle{
	height:30px;
	line-height: 30px;
	text-align: center;
	font-weight: bold;
}
</style>